package com.gs.controller;

import com.gs.entity.User;

import com.gs.service.UserService;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @ProjectName: springbootmybits
 * @Package: com.gs.controller
 * @Description: java类作用描述
 * @Author: Administrator
 * @CreateDate: 2018/5/18 16:20
 * @UpdateUser: Administrator
 * @UpdateDate: 2018/5/18 16:20
 * @UpdateRemark: The modified content
 * @Version: 1.0
 **/
@RestController
public class IndexController {
    private org.slf4j.Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private UserService userService;


    @RequestMapping("/getUserInfo")
    public List<User> getUserInfo() {
        List<User> user = userService.getUserInfo();
        System.out.println(user.toString());
        return user;
    }


    @RequestMapping("/saveUserInfo")
    public User saveUserInfo() {
        User uu = new User();
        uu.setUserId(80);
        uu.setUserName("王s34543");
        uu.setPassword("12323242313324342");
        uu.setPhone("24324324");
        try{
            userService.addUser(uu);

        }catch (Exception e){
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            logger.info("保存用户信息出错："+sw);

        }

        System.out.println(uu.toString());
        logger.info("获得的内容是："+uu.toString());
        return uu;
    }



    @RequestMapping("/saveOrUpdateUserInfo")
    public User saveOrUpdateUserInfo() {
        List<User>  mylist = new ArrayList<User>();
        User uu = new User();
        uu.setUserId(81);
        uu.setUserName("234234");
        uu.setPassword("324324");
        uu.setPhone("sdfdsf");
        mylist.add(uu);

        User uu1 = new User();
        uu1.setUserId(1);
        uu1.setUserName("3432");
        uu1.setPassword("324423");
        uu1.setPhone("sdgsdgsdg");
        mylist.add(uu1);
        try{
            userService.insertOrUpdateUsers(mylist);

        }catch (Exception e){
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            logger.info("保存用户信息出错："+sw);

        }

        System.out.println(uu.toString());
        logger.info("获得的内容是："+uu.toString());
        return uu;
    }


    @RequestMapping(value = "/all/{pageNum}/{pageSize}", produces = {"application/json;charset=UTF-8"})
    public Object findAllUser(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize){

        return userService.findAllUser(pageNum,pageSize);
    }

}
